package com.eedi.framework.mail.controller.sys;


import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.mail.controller.sys.vo.account.MailAccountPageReq;
import com.eedi.framework.mail.controller.sys.vo.account.MailAccountSaveReq;
import com.eedi.framework.mail.controller.sys.vo.account.MailAccountSimpleResp;
import com.eedi.framework.mail.service.SysMailAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.eedi.framework.common.pojo.CommonResult;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import com.eedi.framework.mail.controller.sys.vo.account.MailAccountResp;
import static com.eedi.framework.common.pojo.CommonResult.success;
import com.eedi.framework.mail.dal.dataobject.SysMailAccountDO;
import com.eedi.framework.common.util.object.BeanUtils;

@Tag(name = "系统界面 - 邮箱账号")
@RestController
@RequestMapping("/mail-account")
public class MailAccountController {

    @Resource
    private SysMailAccountService sysMailAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建邮箱账号")
    @PreAuthorize("@ss.hasPermission('sys:mail-account:create')")
    public CommonResult<String> createMailAccount(@Valid @RequestBody MailAccountSaveReq createReqVO) {
        return success(sysMailAccountService.createMailAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改邮箱账号")
    @PreAuthorize("@ss.hasPermission('sys:mail-account:update')")
    public CommonResult<Boolean> updateMailAccount(@Valid @RequestBody MailAccountSaveReq updateReqVO) {
        sysMailAccountService.updateMailAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除邮箱账号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sys:mail-account:delete')")
    public CommonResult<Boolean> deleteMailAccount(@RequestParam String id) {
        sysMailAccountService.deleteMailAccount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得邮箱账号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:mail-account:get')")
    public CommonResult<MailAccountResp> getMailAccount(@RequestParam("id") String id) {
        SysMailAccountDO account = sysMailAccountService.getMailAccount(id);
        return success(BeanUtils.toBean(account, MailAccountResp.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得邮箱账号分页")
    @PreAuthorize("@ss.hasPermission('sys:mail-account:query')")
    public CommonResult<PageResult<MailAccountResp>> getMailAccountPage(@Valid MailAccountPageReq pageReqVO) {
        PageResult<SysMailAccountDO> pageResult = sysMailAccountService.getMailAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MailAccountResp.class));
    }

    @GetMapping({"/list-all-simple", "simple-list"})
    @Operation(summary = "获得邮箱账号精简列表")
    public CommonResult<List<MailAccountSimpleResp>> getSimpleMailAccountList() {
        List<SysMailAccountDO> list = sysMailAccountService.getMailAccountList();
        return success(BeanUtils.toBean(list, MailAccountSimpleResp.class));
    }

}
